using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using System.Threading;

namespace SimExpApp
{
    public delegate void DelegateCloseSplash();

    public class SplashForm : Form
    {	
        public SplashForm()
        {
            CanClose = false;

            InitializeComponent();

            // ====================================================================================
            // Setup the form
            // ==================================================================================== 
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.ShowInTaskbar = false;
            this.TopMost = true;

            // tie up the events
            this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.SplashForm_KeyUp);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.SplashForm_Paint);
            this.MouseDown += new MouseEventHandler(SplashForm_MouseClick);

            //// resize the form to the size of the iamge
            //this.Width = m_bmp.Width;
            //this.Height = m_bmp.Height;

            // center the form
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

            // thread handling
            m_delegateClose = new DelegateCloseSplash(InternalCloseSplash);
        }

        // this can be used for About dialogs
        public static void ShowModal()
        {
            m_instance = new SplashForm();
            m_instance.TopMost = false;
            m_instance.CanClose = true;
            m_instance.ShowDialog();
        }

        public static void StartSplash(Form parent)
        {
            m_parent = parent;
            Thread InstanceCaller = new Thread(new ThreadStart(MySplashThreadFunc));
            InstanceCaller.Start();
        }

        public static void CanCloseNow(bool b)
        {
            if (m_instance != null)
                m_instance.CanClose = b;
        }

        // Call this at the end of your apps initialization to close the splash screen
        public static void CloseSplash()
        {
            if (m_instance != null)
            {
                m_instance.Invoke(m_instance.m_delegateClose);
                m_parent.Activate();
            }
        }

        #region Dispose
        protected override void Dispose( bool disposing )
        {
            base.Dispose( disposing );
            m_instance = null;
        }
        #endregion // Dispose

        #region Threading code
        // ultimately this is called for closing the splash window
        void InternalCloseSplash()
        {
            this.Close();
            this.Dispose();
        }
        // this is called by the new thread to show the splash screen
        private static void MySplashThreadFunc()
        {
            m_instance = new SplashForm();
            m_instance.TopMost = false;
            m_instance.ShowDialog();
        }
        #endregion // Multithreading code

        #region Event Handlers

        void SplashForm_MouseClick(object sender, MouseEventArgs e)
        {
            if (CanClose)
                this.InternalCloseSplash();
        }

        private void SplashForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            //e.Graphics.DrawImage(m_bmp, 0,0);
        }

        private void SplashForm_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (CanClose && (e.KeyCode == Keys.Escape))
                this.InternalCloseSplash();
        }
        #endregion // Event Handlers

        private static Form m_parent;
        private static SplashForm m_instance;
        private DelegateCloseSplash m_delegateClose;
        private PictureBox pictureBox1;
        private bool CanClose;

        private void InitializeComponent()
        {
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.pictureBox1.Image = global::SimExpApp.Properties.Resources.Splash;
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(300, 200);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.SplashForm_MouseClick);
            // 
            // SplashForm
            // 
            this.ClientSize = new System.Drawing.Size(300, 200);
            this.Controls.Add(this.pictureBox1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "SplashForm";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }
    }
}
